<?php

/* **************************************************************************
EstateAgent Improved - a Joomla Component
File:					upload.gallery.php
Copyright:			2004 - 2006 by Darko Selesi

Year: 2007
Improved by: Manos Orfanoudakis - "vosk"
Homepage: 		http://www.eaimproved.eu
Version: 			1.3.x                     
License:			GNU/GPL http://www.gnu.org/copyleft/gpl.html
*************************************************************************** */

defined( '_VALID_MOS' ) or die( 'Direct Access to this location is not allowed.' );

require("resize.gallery.php");



// File Upload
if (isset($_FILES['userfile'])) {
			
	$cfg 		= $_POST;
	
	// is this the first image of this object?
	$sql = "SELECT count(id) FROM #__estateagent_images WHERE objid = $objid";
	$database->setQuery($sql);
	$imgcount=$database->loadResult();
	
	if(	$imgcount+1 > $eaconf->ea_maxpics&&$eaconf->ea_maxpics!=0){
		echo "<script> alert('Maximum Reached - You can`t upload more pictures for this object!'); window.history.go(-1); </script>\n";
        exit();	
	}					
	$src_file	= (isset($_FILES['userfile']['tmp_name']) ? $_FILES['userfile']['tmp_name'] : "");
	
	// check individual settings	
	$cfg['picpath']			= (trim($cfg['picpath']) != trim($eaconf->ea_picpath))? $cfg['picpath'] : $eaconf->ea_picpath;
	$cfg['maxpicsize'] 	= ($cfg['maxpicsize'] != $eaconf->ea_maxpicsize)? $cfg['maxpicsize'] : $eaconf->ea_maxpicsize;
   
	$cfg['imgwidth']		= ($cfg['imgwidth'] != $eaconf->ea_imgwidth)? $cfg['imgwidth'] : $eaconf->ea_imgwidth;
	$cfg['imgheight']		= ($cfg['imgheight'] != $eaconf->ea_imgheight)? $cfg['imgheight'] : $eaconf->ea_imgheight;
	$cfg['imageprtn']		= (isset($cfg['imgprtn']))? 1 : 0;
	$cfg['imgqulty']		= ($cfg['imgqulty'] != $eaconf->ea_imgqulty)? $cfg['imgqulty'] : $eaconf->ea_imgqulty;
    
	$cfg['createTb']		= (isset($cfg['createthumb']))? 1:0;
	$cfg['tbwidth']			= ($cfg['tbwidth'] != $eaconf->ea_tbwidth)? $cfg['tbwidth'] : $eaconf->ea_tbwidth;
	$cfg['tbheight']		= ($cfg['tbheight'] != $eaconf->ea_tbheight)? $cfg['tbheight'] : $eaconf->ea_tbheight;
	$cfg['thumbprtn']		= (isset($cfg['tbprtn']))? 1 : 0;
	$cfg['tbqulty']			= ($cfg['tbqulty'] != $eaconf->ea_tbqulty)? $cfg['tbqulty'] : $eaconf->ea_tbqulty;		

	$dest_dir 				= $mosConfig_absolute_path.$cfg['picpath'];
	$file['type'] 			= strtolower(substr($_FILES['userfile']['name'],-4));
	
	$file['name'] 			= "ea".time();
	$dest_file 				= $dest_dir.$file['name'].$file['type'];

	$file['tbname'] 		= "t".$file['name'];
	$dest_thmb 			= $dest_dir.$file['tbname'].'.jpg';
	/*		
	if (eregi("[^0-9a-zA-Z_ ]", $cfg['title'])) {
			echo "<script> alert('"._EAB_GL_TITLEERROR."'); window.history.go(-1);</script>\n";
			exit();
	}
	*/
	if(filesize($src_file) > (intval($cfg['maxpicsize']) * 1000)) {
			echo "<script> alert('"._EAB_GL_UTB."'); window.history.go(-1);</script>\n";
			exit();	
	}
	if (file_exists($dest_file)) {
			echo "<script> alert('"._EAB_GL_FAE."'); window.history.go(-1);</script>\n";
			exit();
	}

	if ((strcasecmp($file['type'],".gif")) && (strcasecmp($file['type'],".jpg")) && (strcasecmp($file['type'],".png")) &&(strcasecmp($file['type'],".doc")) && (strcasecmp($file['type'],".xls")) && (strcasecmp($file['type'],".ppt")) && (strcasecmp($file['type'],".odt")) && (strcasecmp($file['type'],".odp")) && (strcasecmp($file['type'],".ods")) && (strcasecmp($file['type'],".swf")) && (strcasecmp($file['type'],".pdf")) && (strcasecmp($file['type'],".mpg")) && (strcasecmp($file['type'],".mov")) && (strcasecmp($file['type'],".flv"))	&& (strcasecmp($file['type'],".mp4"))) {
			echo "<script>alert('The file must be gif, png, jpg, swf, doc, xls, ppt, odp, odt, flv, mp4, ods or mpg'); window.history.go(-1);</script>\n";
			exit();     // AVI, MPEG hinzuf?gen  -> Active-X control f?r MSIE -> classid="CLSID:05589FA1-C356-11CE-BF01-00AA0055595A"
	}

	$error = "";
	if($file['type'] == ".jpg" || $file['type'] == ".png" || $file['type'] == ".gif" ) {
		$dest_file 				= $dest_dir.$file['name'].'.jpg';
		# create images & thumbs
		$error = resizeIMG(0,$src_file,$dest_file,$cfg['imgwidth'],$cfg['imgheight'],$cfg['imageprtn'],$cfg['imgqulty']);
	 
		if($cfg['createTb'] == 1) {
    		$error .= resizeIMG(1,$src_file,$dest_thmb,$cfg['tbwidth'],$cfg['tbheight'],$cfg['thumbprtn'],$cfg['tbqulty']);
    	}
    	$file['type']=".jpg";
	}
	else{
		if(@copy($src_file,$dest_file)) $error = "";
		else $error = _EAB_GL_UFS;
	}
  
    if($error == "") {       					  						
    	$pic = new EAImage($database);       					       					
       					       					       					
    	if (!$pic->bind( $_POST )) {
    		echo "<script> alert('".$pic->getError()."'); window.history.go(-1); </script>\n";
    		exit();
    	}
		$pic->title				= trim($cfg['title']);
		$pic->description	= trim($cfg['description']);
		$pic->fname			= $file['name'];
		$pic->type				= $file['type'];
		$pic->path				= trim($cfg['picpath']);
		$pic->owner			= $my->id;
		$pic->ordering		= 0;
		$pic->published			= 1;       					
		//XXX: Multiline textfields EDIT -VOSK
       	$pic->title=str_replace("\r\n","<br />",$pic->title);
		$pic->description=str_replace("\r\n","<br />",$pic->description);
		$pic->fname=str_replace("\r\n","<br />",$pic->fname);
		$pic->type=str_replace("\r\n","<br />",$pic->type);
		$pic->title=str_replace("\n","<br />",$pic->title);
		$pic->description=str_replace("\n","<br />",$pic->description);
		$pic->fname=str_replace("\n","<br />",$pic->fname);
		$pic->type=str_replace("\n","<br />",$pic->type);
		//XXX: Multiline textfields EDIT -VOSK
    
					
		if (!$pic->check()) {
    		echo "<script> alert('".$pic->getError()."'); window.history.go(-1); </script>\n";
    		exit();
    	}
    	if (!$pic->store()) {
    		echo "<script> alert('".$pic->getError()."'); window.history.go(-1); </script>\n";
    		exit();
    	}
    	$pic->checkin();
       				
       	$pic->updateOrder( "objid=$objid AND type='$pic->type'" );       				
       	      				
       	$msg = "File Uploaded Successfull!";
					
		mosRedirect("index$ino.php?option=com_estateagent&act=gallery&action=upload&objid=$pic->objid&msg=$msg");
	}
    else{
    	echo "<script> alert('".$error."'); window.history.go(-1); </script>\n";
    }			
}
else{
		echo "<script> alert('"._EAB_GL_NOFILE."'); window.history.go(-1); </script>\n";	
		}	
		


?>